<template>
  <div>
    <el-dialog :visible.sync="isShow" width="600px">
      <div slot="title" class="title">{{mode == 'add'?'新增用户':'编辑用户'}}</div>
      <el-form :model="form" label-width="75px" :rules="rules" ref="form">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role_id">
          <el-select v-model="form.role_id" placeholder="请选择角色">
            <el-option value="2" label="管理员"></el-option>
            <el-option value="3" label="老师"></el-option>
            <el-option value="4" label="学生"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option value="0" label="禁用"></el-option>
            <el-option value="1" label="启用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户备注" prop="remark">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="footer">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addUser, editUser } from "@/api/user.js";
export default {
  data() {
    return {
      isShow: false,
      form: {
        username: "", //	是	string	用户名
        email: "", //	是	string	邮箱
        phone: "", //	是	string	手机号
        role_id: "", //	是	string	角色 、2 管理员、3 老师、4 学生
        status: "", //	否	string	1(启用)0(禁用)
        remark: "", //	否	string	备注
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          {
            validator(rule, val, callback) {
              let reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
              if (reg.test(val)) {
                callback();
              } else {
                callback(new Error("请输入正确的邮箱"));
              }
            },
            trigger: "blur",
          },
        ],
        phone: [
          { required: true, message: "请输入电话", trigger: "blur" },
          {
            validator(rule, val, callback) {
              let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (reg.test(val)) {
                callback();
              } else {
                callback(new Error("请输入正确的手机号码"));
              }
            },
            trigger: "blur",
          },
        ],
        role_id: [
          { required: true, message: "请选择角色", trigger: "blur" },
          {
            validator(rule, val, callback) {
              if (val) {
                callback();
              } else {
                callback(new Error("请选择角色"));
              }
            },
            trigger: "blur",
          },
        ],
      },
      mode: "add",
    };
  },
  methods: {
    submit() {
      this.$refs.form.validate((res) => {
        if (res) {
          if (this.mode == "add") {
            addUser(this.form).then(() => {
              this.$message.success("新增成功");
              this.isShow = false;
              this.$parent.search();
            });
          } else if (this.mode == "edit") {
            editUser(this.form).then(() => {
              this.$message.success("编辑成功");
              this.isShow = false;
              this.$parent.search();
            });
          }
        } else {
          this.$message.error("验证失败");
        }
      });
    },
  },
  watch: {
        isShow(newVal){
            if(newVal == false){
                this.form= {
                    username: "", //	是	string	用户名
                    email: "", //	是	string	邮箱
                    phone: "", //	是	string	手机号
                    role_id: "", //	是	string	角色 、2 管理员、3 老师、4 学生
                    status: "", //	否	string	1(启用)0(禁用)
                    remark: "", //	否	string	备注
                }
                this.$nextTick(()=>{
                    this.$refs.form.clearValidate()
                })
            }
        }
    }
};
</script>

<style lang="less" scoped>
.title {
  height: 53px;
  line-height: 53px;
  text-align: center;
  color: #fff;
  background: linear-gradient(to right, #01c5fa, #1395fa);
  
}
.el-dialog__headerbtn .el-dialog__close {
  color: #fff!important;
}
.footer {
  text-align: center;
}
</style>